package com.tn.rtdata.examples.stockticker.client;

import javax.swing.*;
import java.awt.*;
import java.util.Map;

import com.tn.rtdata.FillException;
import com.tn.rtdata.examples.stockticker.StockTick;
import com.tn.rtdata.util.LiveMap;

/**
 *
 */
public class ClientFrame extends JFrame
{
  private static final Dimension DEFAULT_SIZE = new Dimension(300, 200);

  /**
   * Creates a new <code>ClientFrame</code>.
   */
  public ClientFrame(String title, Map<String, StockTick> stockTicks) throws HeadlessException
  {
    setTitle(title);
    setSize(DEFAULT_SIZE);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    createControls(stockTicks);

    try
    {
      if (stockTicks instanceof LiveMap)
      {
        ((LiveMap)stockTicks).start();
      }
    }
    catch (FillException e)
    {
      JOptionPane.showMessageDialog(this, "An error occurred starting the data fill with message: " + e.getMessage());
    }

    setVisible(true);
  }

  private void createControls(Map<String, StockTick> stockTicks)
  {
    JTable stockTicksTable = new JTable(new StockTickTableModel(stockTicks));

    JScrollPane stockTickScrollPane = new JScrollPane(stockTicksTable);

    JPanel stockTicksPanel = new JPanel(new BorderLayout());
    stockTicksPanel.add(stockTickScrollPane, BorderLayout.CENTER);

    JPanel dataEventsPanel = new JPanel();
    //dataEventsPanel.add()

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Stock Ticks", stockTicksPanel);
    tabbedPane.addTab("Data Events", dataEventsPanel);

    getContentPane().add(tabbedPane);
  }
}
